package com.weitu.train.record.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.weitu.train.record.entity.Record;
import com.weitu.train.record.entity.RecordUser;
import com.weitu.train.record.mapper.RecordMapper;
import com.weitu.train.record.service.RecordService;
import java.util.List;
import java.util.Map;

import com.weitu.util.IDGenerator;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public Record selectById(String id) {
        return this.recordMapper.selectById(id);
    }

    @Override
    @Transactional
    public int deleteById(String id) {
        this.recordMapper.deleteUser(id);
        return this.recordMapper.deleteById(id);
    }

    @Override
    public int updateByIdSelective(Map<String, Object> record) {
        return this.recordMapper.updateByIdSelective(record);
    }

    @Override
    public int updateById(Record record) {
        return this.recordMapper.updateById(record);
    }

    @Override
    public int insert(Record record) {
        return this.recordMapper.insert(record);
    }

    @Override
    public int insertSelective(Map<String, Object> record) {
        return this.recordMapper.insertSelective(record);
    }

    @Override
    @Transactional
    public boolean insertRecord(Map<String,Object> map) {
        int rint=this.recordMapper.insertSelective(map);
        boolean uboo=addUser(map);
        boolean rboo=false;
        if (rint>0){
            rboo=true;
        }
        return uboo&&rboo;
    }

    @Override
    @Transactional
    public boolean updateRecord(Map<String, Object> map) {
        int rint=this.recordMapper.updateByIdSelective(map);
        boolean uboo=addUser(map);
        boolean rboo=false;
        if (rint>0){
            rboo=true;
        }
        return uboo&&rboo;
    }

    @Override
    public List<Record> selectByPlanId(@Param("id") String id) {
        return this.recordMapper.selectByPlanId(id);
    }

    @Override
    public List<RecordUser> selectAllUserById(Map<String, Object> map) {
        return this.recordMapper.selectAllUserById(map);
    }

    @Override
    public int selectAllUserCountById(Map<String, Object> map) {
        return this.recordMapper.selectAllUserCountById(map);
    }

    @Override
    public int deleteUserById(String id) {
        return this.recordMapper.deleteUserById(id);
    }

    @Override
    public List<RecordUser> selectAllUserListById(String recordId) {
        return this.recordMapper.selectAllUserListById(recordId);
    }

    @Override
    public List<Map<String, Object>> selectMyRecords(Map<String, Object> map) {
        return this.recordMapper.selectMyRecords(map);
    }

    @Override
    public int selectMyRecordsCount(Map<String, Object> map) {
        return this.recordMapper.selectMyRecordsCount(map);
    }

    @Override
    public List<Record> selectAccount(Map<String, Object> map) {
        return this.recordMapper.selectAccount(map);
    }

    @Override
    public int selectAccountCount(Map<String, Object> map) {
        return this.recordMapper.selectAccountCount(map);
    }

    private boolean addUser(Map<String,Object> paramMap){
        boolean boo=false;
        String recordId=paramMap.get("id").toString();
        String dateTime=paramMap.get("addTime").toString();
        String loginUserId=paramMap.get("addUserId").toString();
        String loginUserDeptId=paramMap.get("addDeptId").toString();
        String planId=paramMap.get("planId").toString();
        if (paramMap.get("personTb")!=null){
            String dataStr = paramMap.get("personTb").toString();
            List<Map> list=addEditTableLists(dataStr,recordId,dateTime,loginUserId,loginUserDeptId,planId);
            paramMap.put("pList",list);
            int dint=this.recordMapper.deleteUser(recordId);
            int aint=this.recordMapper.insertUser(paramMap);
            if (aint>0){
                boo=true;
            }
        }else {
            int dint=this.recordMapper.deleteUser(recordId);
            boo=true;
        }
        return boo;
    }

    private List<Map> addEditTableLists(String dataStr,String recordId,String dateTime,String loginUserId,String loginUserDeptId,String planId){
        List<Map> list = JSONArray.parseArray(dataStr,Map.class);
        for(Map m:list){
            String uuid = IDGenerator.getId();
            m.put("id",uuid);
            m.put("delState",0);
            m.put("recordId",recordId);
            m.put("addTime", dateTime);
            m.put("addUserId", loginUserId);
            m.put("addDeptId", loginUserDeptId);
            m.put("planId", planId);
        }
        return list;
    }
}